<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="资产种类" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择资产种类"
          clearable
          class="!w-200px"
        >
          <el-option label="零钱" value="money" />
          <el-option label="红包" value="red_packet" />
          <el-option label="佣金" value="commission" />
          <el-option label="联盟豆" value="cryptocurrency_origin" />
          <el-option label="收益豆" value="cryptocurrency_increment" />
          <el-option label="分红" value="bonus" />
          <el-option label="消费金" value="interest" />
        </el-select>
      </el-form-item>
      <el-form-item label="收支情况" prop="mode">
        <el-select
          v-model="queryParams.mode"
          placeholder="请选择收支情况"
          clearable
          class="!w-200px"
        >
          <el-option label="收入" :value="1" />
          <el-option label="支出" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table :data="list">
      <el-table-column label="用户" align="center" prop="userInfo" min-width="120px">
        <template #default="scope">
          <span class="ml-5px">{{ scope.row.user?.nickname || '匿名' }}</span>
          <br />
          <img :src="scope.row.user?.avatar || './avatar.png'" class="w-40px" />
        </template>
      </el-table-column>
      <el-table-column label="种类" align="center" prop="category" width="80">
        <template #default="scope">
          <span v-if="scope.row.category == 'money'">零钱</span>
          <span v-else-if="scope.row.category == 'red_packet'">红包</span>
          <span v-else-if="scope.row.category == 'commission'">佣金</span>
          <span v-else-if="scope.row.category == 'cryptocurrency_origin'">联盟豆</span>
          <span v-else-if="scope.row.category == 'cryptocurrency_increment'">收益豆</span>
          <span v-else-if="scope.row.category == 'bonus'">分红</span>
          <span v-else-if="scope.row.category == 'interest'">消费金</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="变动原因" align="center" prop="title" min-width="120">
        <template #default="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="变动内容" align="left" prop="number" min-width="180">
        <template #default="scope">
          <div class="flex flex-row flex-wrap">
            <div class="flex items-center">
              <span v-if="scope.row.mode == 0">支出</span>
              <span v-else-if="scope.row.mode == 1">收入</span>
              <span v-else>变动</span>
              <span
                v-if="
                  scope.row.category == 'money' ||
                  scope.row.category == 'red_packet' ||
                  scope.row.category == 'commission' ||
                  scope.row.category == 'bonus' ||
                  scope.row.category == 'interest'
                "
                :class="scope.row.mode == 1 ? 'text-red-500 font-bold' : 'text-green-500 font-bold'"
                >{{ fenToYuan(Math.abs(scope.row.number)) }}元</span
              >
              <span
                v-else
                :class="scope.row.mode == 1 ? 'text-red-500 font-bold' : 'text-green-500 font-bold'"
                >{{ Math.abs(scope.row.number) }}个</span
              >
            </div>
            <div>，</div>
            <div>
              <span>剩余</span>
              <span
                v-if="
                  scope.row.category == 'money' ||
                  scope.row.category == 'red_packet' ||
                  scope.row.category == 'commission' ||
                  scope.row.category == 'bonus' ||
                  scope.row.category == 'interest'
                "
                class="text-black font-bold"
                >{{ fenToYuan(scope.row.balance) }}元</span
              >
              <span v-else class="text-black font-bold">{{ scope.row.balance }}个</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="变动说明" align="left" prop="mark" min-width="200">
        <template #default="scope">
          <div class="multi-hidden">{{ scope.row.mark }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>
<script setup lang="ts">
import * as PayBillApi from '@/api/pay/bill'
import { fenToYuan } from '@/utils'
import { dateFormatter } from '@/utils/formatTime'

defineOptions({ name: 'PayBill' })

const props = defineProps({
  userId: {
    type: [Number, String]
  }
})

const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const loading = ref(false)
const queryParams = reactive<any>({
  pageNo: 1,
  pageSize: 10,
  userId: '',
  relateId: null,
  mode: null,
  title: null,
  category: null,
  type: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PayBillApi.getPayBillPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  queryParams.userId = props.userId || ''
  await getList()
})
</script>
